package kr.ltkblue;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpClientPost {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		// HttpClient 생성
		DefaultHttpClient httpclient = new DefaultHttpClient();

		// url
		HttpPost httpPost = new HttpPost(
				"https://www.google.com/accounts/ClientLogin");
		// 파라메터 리스트
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		// 파라메터 설정
		// new BasicNameValuePair(키, 값)

		nvps.add(new BasicNameValuePair("Email", "	"));
		nvps.add(new BasicNameValuePair("Passwd", ""));
		nvps.add(new BasicNameValuePair("accountType", "GOOGLE"));
		nvps.add(new BasicNameValuePair("source", "Google-cURL-Example"));
		nvps.add(new BasicNameValuePair("service", "ac2dm"));

		httpPost.setEntity(new UrlEncodedFormEntity(nvps));

		// 응답
		HttpResponse response = httpclient.execute(httpPost);

		try {
			// 응답 결과
			System.out.println(response.getStatusLine());
			System.out.println("----------------------------------------");

			HttpEntity entity = response.getEntity();

			if (entity != null) {
				System.out.println("Response content length: "
						+ entity.getContentLength());

				// 콘텐츠를 읽어들임.
				BufferedReader rd = new BufferedReader(new InputStreamReader(
						entity.getContent()));

				String line = "";
				while ((line = rd.readLine()) != null) {
					// 콘텐츠 내용
					 System.out.println(line);
				}
			}

//			EntityUtils.consume(entity);
		} finally {
//			httpPost.releaseConnection();
		}
	}

}